package com.example.orderassembly.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cloud.api.GoodApi;
import com.woniu.cloud.api.StoreGoodApi;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TGood;
import com.woniu.cloud.entity.TStoreGood;
import com.woniuxy.entity.ProductOrderDetail;
import com.example.orderassembly.mapper.ProductOrderDetailMapper;
import com.example.orderassembly.service.IProductOrderDetailService;
import com.woniuxy.form.InsertGoodOrderDetail;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 商品订单详情表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@Service
public class ProductOrderDetailServiceImpl extends ServiceImpl<ProductOrderDetailMapper, ProductOrderDetail> implements IProductOrderDetailService {
    @Resource
    ProductOrderDetailMapper productOrderDetailMapper;

    @Resource
    StoreGoodApi storeGoodApi;

    @Override
    public List<ProductOrderDetail> getProductOrderDetailByProductOrderId(Integer goodOrderId) {
        QueryWrapper<ProductOrderDetail> productOrderDetailQueryWrapper = new QueryWrapper<>();
        productOrderDetailQueryWrapper.eq("product_order_detail_order_id",goodOrderId);
        List<ProductOrderDetail> productOrderDetails = productOrderDetailMapper.selectList(productOrderDetailQueryWrapper);
        return productOrderDetails;
    }

    @Override
    public Boolean insertProductOrderDetail(InsertGoodOrderDetail insertGoodOrderDetail) {
        ProductOrderDetail productOrderDetail = new ProductOrderDetail();
        //todo 通过jwt 获取用户id
        Integer userId = insertGoodOrderDetail.getUserId();
        //设置订单id
        productOrderDetail.setProductOrderDetailOrderId(insertGoodOrderDetail.getGoodOrderId());
        //设置用户id
        productOrderDetail.setProductOrderDetailUserId(userId);

        HashMap<Integer,Integer> map = insertGoodOrderDetail.getGoodAttribute();
        Set<Integer> set = map.keySet();
//        for (Integer key:set){
//            System.out.println(key);
//            System.out.println(map.get(key)+"==="+map.get(key).getClass());
//        }
//        Thread.sleep(20000);
        Integer num = 0;
        for (Integer key:set) {
            //设置数量
            productOrderDetail.setProductOrderDetailNum(map.get(key));
            //根据id查询 商品信息
            Result goodByGoodId = storeGoodApi.getStoreGoodByStoreGoodId(key);
            TStoreGood data  = JSON.parseObject(JSON.toJSONString(goodByGoodId.getData()), TStoreGood.class);
            //设置价格
            productOrderDetail.setProductOrderDetailPrice(new BigDecimal(data.getStoreGoodPrice()));
            //设置小计
            productOrderDetail.setProductOrderDetailTotal(new BigDecimal(data.getStoreGoodPrice()*map.get(key)));
            //设置商品id
            productOrderDetail.setProductOrderDetailStoreGoodId(key);
            //设置商品名
            productOrderDetail.setProductOrderDetailGoodName(data.getStoreGoodName());
            //插入数据库
            num += productOrderDetailMapper.insert(productOrderDetail);
        }
        return set.size()==num;
    }
}
